package com.starnet.yarnmonitor.yarn.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.starnet.yarnmonitor.yarn.vo.YarnWarnOptionVO;
import com.starnet.yarnmonitor.yarn.vo.param.YarnWarnOptionUpdateParam;
import com.starnet.yarnmonitor.yarn.entity.YarnWarnOption;
import com.starnet.yarnmonitor.yarn.service.YarnWarnOptionService;
import com.starnet.yarnmonitor.yarn.utils.PageUtils;
import com.starnet.yarnmonitor.yarn.vo.param.YarnWarnOptionAddParam;
import com.starnet.yarnmonitor.yarn.vo.result.PageResult;
import com.starnet.yarnmonitor.yarn.vo.result.PageVO;
import com.starnet.yarnmonitor.yarn.vo.result.Result;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@RestController
@RequestMapping("/yarn/warn/options")
public class YarnWarnOptionController {
    @Autowired
    YarnWarnOptionService yarnWarnOptionService;

    @ApiOperation(value = "获取所有告警阈值设置")
    @GetMapping("/options")
    public PageResult getPagedOptions(@RequestParam(value = "cur", required = false) Integer cur, @RequestParam(value = "size", required = false) Integer size) {
        IPage<YarnWarnOptionVO> yarnWarnOptions = yarnWarnOptionService.getAllOptions(cur, size);
        PageVO pageVO = PageUtils.getPageVOFromIPage(yarnWarnOptions);
        return PageResult.success(yarnWarnOptions.getRecords(), pageVO);
    }

    @ApiOperation(value = "新增告警阈值设置")
    @PostMapping("/options")
    public Result addNewOptions(@RequestBody @Valid YarnWarnOptionAddParam yarnWarnOptionAddParam) {
        yarnWarnOptionService.addNewOptions(yarnWarnOptionAddParam);
        return new Result(true, "新增预警请联系后端重新开发", null);
    }

    @ApiOperation(value = "删除告警阈值设置")
    @DeleteMapping("/options")
    public Result deleteOptionById(@RequestParam(value = "id", required = true) Long id) {
        yarnWarnOptionService.deleteOptionById(id);
        return Result.success();
    }

    @ApiOperation(value = "修改阈值设置")
    @PutMapping("/options")
    public Result updateOption(@RequestBody @Valid YarnWarnOptionUpdateParam yarnWarnOptionUpdateParam) {
        yarnWarnOptionService.updateOption(yarnWarnOptionUpdateParam);
        return Result.success();
    }
}
